﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucListProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
                {
                 //   Functions.ddlDatabinder(drpLoaiSanPham, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new cmsLoaiSanPhamBL().SelectAll(0));
                    bindToDropDown(drpLoaiSanPham);
                    Functions.ddlDatabinder(drpNhanHieu, cmsNhanHieuDO.NHANHIEUID_FIELD, cmsNhanHieuDO.TENNHANHIEU_FIELD, new cmsNhanHieuBL().SelectAll());
                    gvProductDataSource();
                }
        }

        protected void gvProductDataSource()
        {
         //   gvProduct.DataSource = new cmsSanPhamBL().SelectAll();
            //if(txtProductFilter.Text.Trim().Equals(""))
            gvProduct.DataSource = new cmsSanPhamBL().Filter(int.Parse(drpLoaiSanPham.SelectedValue),txtProductFilter.Text.Trim());// .Filter(int.Parse(drpLoaiSanPham.SelectedValue), txtProductFilter.Text.Trim());
            gvProduct.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=Product");
        }


        protected void gvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsSanPhamBL().Delete(new cmsSanPhamDO { SanPhamID = Convert.ToInt64(gvProduct.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa bản ghi thành công", Request.Url.ToString());
        }

        protected void gvProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            int SanPhamID = int.Parse(gvProduct.DataKeys[gvProduct.SelectedIndex].Value.ToString());
            Response.Redirect("Default.aspx?Page=Product&ProductID=" + SanPhamID.ToString());
        }

        protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProduct.PageIndex = e.NewPageIndex;
            gvProductDataSource();
        }

        protected void drpLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            //gvProduct.DataSource = new cmsSanPhamBL().SelectAllbyLoaiSanPhamID(int.Parse(drpLoaiSanPham.SelectedValue));
            //gvProduct.DataBind();
            gvProductDataSource();
        }

        protected void drpNhanHieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvProduct.DataSource = new cmsSanPhamBL().SelectbyNhanHieuID(int.Parse(drpNhanHieu.SelectedValue));
            gvProduct.DataBind();
        }

        protected void btnUpdateAll_Click(object sender, EventArgs e)
        {
           for(int i=0;i<gvProduct.Rows.Count;i++)
           {
               TextBox txtRows = (TextBox)gvProduct.Rows[i].FindControl("txtDonGia");
               txtRows.Visible = true;

               Label lblRows = (Label)gvProduct.Rows[i].FindControl("lblDonGia");
               lblRows.Visible = false;
           }
           btnUpdate.Visible = true;
           btnUpdateAll.Visible = false;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            cmsSanPhamDO obj = new cmsSanPhamDO();
            int stt = gvProduct.Rows.Count;
            int[] spid = new int[stt];
            long[] dongia = new long[stt];

            for (int i = 0; i < gvProduct.Rows.Count; i++)
            {
                TextBox txtRows = (TextBox)gvProduct.Rows[i].FindControl("txtDonGia");
                txtRows.Visible = true;

                spid[i] = Int32.Parse(gvProduct.DataKeys[i].Value.ToString());
                dongia[i] = Int64.Parse(txtRows.Text);

                //obj = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = Int64.Parse(gvProduct.DataKeys[i].Value.ToString())});

                //try
                //{
                //    obj.DonGia = long.Parse(txtRows.Text);
                //}
                //catch 
                //{
                //}
                //new cmsSanPhamBL().Update(obj);
                //gvProductDataSource();
            }

            new cmsSanPhamBL().UpdateAll(stt, spid, dongia);
            gvProductDataSource();
            btnUpdate.Visible = false;
            btnUpdateAll.Visible = true;
        }

        /*
         - Mỗi lần click -> buil thêm 1 câu lệnh: Update cmsSanPham set DonGia = xxx where SanPhamID = yyy
         - Sau khi for -> Execute câu lệnh trên
         */

        protected void gvProduct_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvProduct.EditIndex = e.NewEditIndex;
            gvProductDataSource();
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsLoaiSanPhamBL().SelectAll(0);
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["LoaiSanPhamID"].ToString();
                    item.Text = "+ " + dr["TenLoaiSanPham"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["LoaiSanPhamID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["TenLoaiSanPham"].ToString();
                    listChild.Value = dr["LoaiSanPhamID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                    GetChildItems(dr["LoaiSanPhamID"].ToString(), dtTemp, ddl);
                }
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            gvProductDataSource();
        }
    }
}